package com.wzm.moviepic.slidingmenu;



import java.io.File;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManager.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

import com.umeng.analytics.MobclickAgent;
import com.umeng.update.UmengUpdateAgent;
import com.wzm.manager.AppConfig;
import com.wzm.manager.AppManager;
import com.wzm.manager.ManagerTab;
import com.wzm.manager.Tag;
import com.wzm.moviepic.AppApplication;
import com.wzm.moviepic.R;
import com.wzm.moviepic.fragment.LocalFileFragment;
import com.wzm.moviepic.fragment.MainFragment;
import com.wzm.moviepic.fragment.MenuFragment;
import com.wzm.moviepic.fragment.QR_codeFragment;
import com.wzm.moviepic.fragment.SelectDialog;
import com.wzm.utils.FileUtils;
import com.wzm.utils.NetworkUtils;
import com.wzm.utils.UtilsTools;

public class Main1Activity extends BaseActivity implements ManagerTab
{
   
    private Button btn_left;
    private Button btn_right;
    private Context mContext = null;
   
    private SelectDialog selectDialog = null;
    private Fragment mainFragment = null;
    private Fragment menuFragment = null;
    private Fragment localFragment = null;
    private Fragment qrcodeFragment = null;

    private RadioGroup mMenuBar = null;
   
    private FragmentTransaction fTransaction = null;
    private FragmentManager fManager = null;
    
    //底部渐现
  	private Animation animation_up = null;
  	//底部渐隐
  	private Animation animation_down = null;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        //友盟更新
        UmengUpdateAgent.update(this);
       
        mContext = this;
        setContentView(R.layout.layout_slidemenu_center);
        btn_left = (Button)findViewById(R.id.showLeft);
        btn_left.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                showleft();
            }
        });
        btn_right = (Button)findViewById(R.id.showRight);
        btn_right.setOnClickListener(new OnClickListener() {
           
            @Override
            public void onClick(View v) {
                showright();
            }
        });
       
        int isFirst = UtilsTools.getTagInt(mContext, Tag.ISLISTFIRST);
        if(isFirst==0)
        {
            showDialog();
        }
        //添加
        if(findViewById(R.id.main_frame)!=null)
        {
            fManager = this.getSupportFragmentManager();
            fTransaction = fManager.beginTransaction();
            mainFragment = new MainFragment(this);
            menuFragment = new MenuFragment(this);    
            localFragment = new LocalFileFragment(this);
            qrcodeFragment = new QR_codeFragment(this);
            
            fTransaction.add(R.id.main_frame, qrcodeFragment);
            fTransaction.add(R.id.main_frame, localFragment);
            fTransaction.add(R.id.main_frame, menuFragment);
            fTransaction.add(R.id.main_frame, mainFragment);
            fTransaction.commit();
        }
        animation_up = AnimationUtils.loadAnimation(mContext, R.anim.fadein);
		animation_down = AnimationUtils.loadAnimation(mContext, R.anim.fadeout);
		
        mMenuBar = (RadioGroup)findViewById(R.id.main_menu);
        mMenuBar.setOnCheckedChangeListener(new OnCheckedChangeListener() {           
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                // TODO Auto-generated method stub
                //mButtomBar.getChildAt(oldpos).setBackgroundResource(R.drawable.home_btn_bg_s);
            	hideIME();
            	showtab();
            	
                switch(checkedId)
                {
                case R.id.radio_new:
                    if(mainFragment!=null)
                    {
                        fTransaction = fManager.beginTransaction();
                        fTransaction.show(mainFragment);
                        fTransaction.hide(menuFragment);
                        fTransaction.hide(localFragment);
                        fTransaction.hide(qrcodeFragment);
                        fTransaction.commit();
                    }
                    break;
                case R.id.radio_search:
                    if(menuFragment!=null)
                    {
                        fTransaction = fManager.beginTransaction();
                        fTransaction.show(menuFragment);
                        fTransaction.hide(mainFragment);
                        fTransaction.hide(localFragment);
                        fTransaction.hide(qrcodeFragment);
                        fTransaction.commit();
                    }
                    break;
                case R.id.radio_downed:
                    if(localFragment!=null)
                    {
                    	fTransaction = fManager.beginTransaction();
                        fTransaction.show(localFragment);
                        fTransaction.hide(mainFragment);
                        fTransaction.hide(menuFragment);
                        fTransaction.hide(qrcodeFragment);
                        fTransaction.commit();
                    }
                    break;
                case R.id.radio_more:
                	if(qrcodeFragment!=null)
                    {
                    	fTransaction = fManager.beginTransaction();
                        fTransaction.show(qrcodeFragment);
                        fTransaction.hide(mainFragment);
                        fTransaction.hide(menuFragment);
                        fTransaction.hide(localFragment);
                        fTransaction.commit();
                    }
                    break;
                }
               
               
            }
        });
       
       
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        //getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
   
    public void showleft()
    {
        slidingMenu.showMenu();
    }
   
    public void showright()
    {
        slidingMenu.showSecondaryMenu();
    }
   
    public void showcenter()
    {
        slidingMenu.showContent();
    }
   
   
    @Override
       public boolean onKeyDown(int keyCode, KeyEvent event) {
           // TODO Auto-generated method stub
           switch (keyCode) {
               case KeyEvent.KEYCODE_BACK:
                   if(!slidingMenu.isMenuShowing())
                       exitApp(mContext);
                   break;
           }
           return super.onKeyDown(keyCode, event);
       }

       public void exitApp(final Context context) {
           AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context);
           alertBuilder.setTitle(context.getString(R.string.app_exit_title))
               .setMessage(this.getString(R.string.app_exit_message))
               .setPositiveButton("退出",
                   new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                          
                           AppManager.getInstance(mContext).saveInfo();
                         
                           if(AppApplication.mNetWorkState!=NetworkUtils.NETWORN_NONE)
                           {
                        	   File ff = new File(Environment.getExternalStorageDirectory().getPath()+AppConfig.IMGCACHE);                       
                               long filesize = 0;
                               try {
                                   filesize = FileUtils.getFileSize(ff);
                               } catch (Exception e) {
                                   // TODO Auto-generated catch block
                                   //e.printStackTrace();
                               }
                               //如果大于10M，自动进行清理
                               if(filesize>1024*1024*10)
                               {
                                   File file = new File(Environment.getExternalStorageDirectory().getPath()+AppConfig.IMGCACHE);
                                   FileUtils.deleteDir(file);
                                   file = null;
                               }
                           }    
                           android.os.Process.killProcess(android.os.Process.myPid());
                       
                       }
                   }
               ).setNegativeButton("取消",
                   new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {
                           dialog.cancel();
                       }
                   }
               );
           alertBuilder.create().show();
       }
      
           public void showDialog()
           {
                  selectDialog = new SelectDialog(this,R.style.dialog,1);//创建Dialog并设置样式主题
                  Window win = selectDialog.getWindow();
                  LayoutParams params = new LayoutParams();
                  params.x = 0;//设置x坐标
                  params.y = 0;//设置y坐标
                  win.setAttributes(params);
                  selectDialog.setCanceledOnTouchOutside(true);//设置点击Dialog外部任意区域关闭Dialog
                  selectDialog.show();
           }
          
          
           @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            super.onPause();
            MobclickAgent.onPause(this);
        }
        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            MobclickAgent.onResume(this);
        }

		@Override
		public void hidetab() {
			// TODO Auto-generated method stub
			if(mMenuBar.getVisibility()==View.VISIBLE){
				mMenuBar.startAnimation(animation_down);
				mMenuBar.setVisibility(View.GONE);
			}
		}

		@Override
		public void showtab() {
			// TODO Auto-generated method stub
			if(mMenuBar.getVisibility()==View.GONE)
			{
				mMenuBar.startAnimation(animation_up);
				mMenuBar.setVisibility(View.VISIBLE);	
			}		
		}

		@Override
		public void hideIME() {
			// TODO Auto-generated method stub
			InputMethodManager inputMethodManager = (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        	inputMethodManager.hideSoftInputFromWindow(mMenuBar.getWindowToken(), 0);
		}

}